# coding: utf-8
from sqlalchemy import Column, INT
from sqlalchemy_dm import VARCHAR, DATE, DATETIME
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类
Base = declarative_base()

# 用户类
class SysUserModel(Base):
    # 用户
    __table_args__ = {'schema': 'ESUNTEST'}
    # 表名
    __tablename__ = 'HX_BI_USER'
    # 表结构
    ID = Column(INT, primary_key=True, autoincrement=True)
    NICKNAME = Column(VARCHAR(256))
    USERNAME = Column(VARCHAR(256))
    PASSWORD = Column(VARCHAR(256))
    STATUS = Column(INT)
    CREATE_TIME = Column(DATETIME)

    @classmethod
    def from_dict(cls, data):
        return cls(
            NICKNAME=data['NICKNAME'],
            USERNAME=data['USERNAME'],
            PASSWORD=data['PASSWORD'],
            STATUS=int(data['STATUS']) if data['STATUS'] is not None else 1,
        )

    def to_all_dict(self):
        return {
            'ID': self.ID,
            'NICKNAME': self.NICKNAME,
            'USERNAME': self.USERNAME,
            'PASSWORD': self.PASSWORD,
            'STATUS': int(self.STATUS),
            'CREATE_TIME': str(self.CREATE_TIME) if self.CREATE_TIME is not None else None
        }

    def to_dict(self):
        return {
            'NICKNAME': self.NICKNAME,
            'USERNAME': self.USERNAME,
            'PASSWORD': self.PASSWORD,
            'STATUS': int(self.STATUS),
        }
